//
//  LGLungFollowTemplateController.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2024/7/5.
//  肺癌-随访计划-计划模版

#import "LGLungFollowTemplateController.h"
//新建随访计划
#import "LGLungFollowSendController.h"
@interface LGLungFollowTemplateController ()<UITableViewDelegate,UITableViewDataSource>
/**   */
@property (nonatomic , strong)UITextField *searchTF;
/**   */
@property (nonatomic , strong)UITableView *tableView;
/**   */
@property (nonatomic , strong)NSArray *dataArray;
/**   */
@property (nonatomic , copy)NSString *keyword;

@end

@implementation LGLungFollowTemplateController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.keyword = @"";
    self.title = @"选择随访计划模板";
    [self.view addSubview:self.searchTF];
    [self.view addSubview:self.tableView];
    [self requestData];
}

-(void)requestData{
    [RequestUtil withPOST:@"/doctor/Careplan/carePlanTemplateList" parameters:@{@"keywords":self.keyword} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
        if (code == 200) {
            self.dataArray = returnData;
            [self.tableView reloadData];
        }
    } error:^(NSError *error) {
        
    }];
}

-(void)textFieldEditingChanged:(UITextField *)textField{
    self.keyword = textField.text.length>0?textField.text:@"";
    [self requestData];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LGLungFollowTemplateCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.dataDic = self.dataArray[indexPath.row];
    if (self.dataArray.count == 1) {
        cell.type = LGPositionTypeSingle;
    }else if (indexPath.row == 0){
        cell.type = LGPositionTypeTop;
    }else if (indexPath.row == self.dataArray.count-1){
        cell.type = LGPositionTypeBottom;
    }else{
        cell.type = LGPositionTypeMiddle;
    }
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [self.searchTF resignFirstResponder];
    LGLungFollowSendController *controller = [[LGLungFollowSendController alloc]init];
    controller.planid = LGNSString(self.dataArray[indexPath.row][@"careplan_id"]);
    [self.navigationController pushViewController:controller animated:YES];
}

-(UITableView *)tableView{
    if(!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, viewPix(45), Screen_W, Screen_H-topBarHeight) style:UITableViewStyleGrouped];
        [_tableView registerClass:[LGLungFollowTemplateCell class] forCellReuseIdentifier:@"cell"];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.tableHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(10))];
    }
    return _tableView;
}

-(UITextField *)searchTF{
    if(!_searchTF){
        _searchTF = [[UITextField alloc]initWithFrame:CGRectMake(viewPix(16), viewPix(6), Screen_W-viewPix(32), viewPix(32))];
        [_searchTF setShadoWithColor:@"#B3C5D9" offset:CGSizeMake(0, 2) opacity:0.15 radius:15];
        _searchTF.backgroundColor = [UIColor whiteColor];
        _searchTF.tintColor = [UIColor colorWithString:@"#333333"];
        _searchTF.textColor = [UIColor colorWithString:@"#333333"];
        _searchTF.textAlignment = NSTextAlignmentLeft;
        _searchTF.keyboardType = UIKeyboardTypeDefault;
        _searchTF.placeholder = @"输入关键字搜索随访计划模板";
        _searchTF.cornerRidus = viewPix(16);
        _searchTF.font = LGFont(15);
        _searchTF.leftViewMode = UITextFieldViewModeAlways;
        _searchTF.leftView = [self leftView];
        [_searchTF addTarget:self action:@selector(textFieldEditingChanged:) forControlEvents:UIControlEventEditingChanged];
    }
    return _searchTF;
}

-(UIView *)leftView{
    UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, viewPix(36), 32)];
    UIImageView *searchIcon = [[UIImageView alloc]initWithFrame:CGRectMake(viewPix(12), 8, 16, 16)];
    searchIcon.image = [UIImage imageNamed:@"searchGray16"];
    [leftView addSubview:searchIcon];
    return leftView;
}

@end



@interface LGLungFollowTemplateCell()

/**   */
@property (nonatomic , strong)UILabel *titleLabel;
/**   */
@property (nonatomic , strong)UIView *lineView;
/**   */
@property (nonatomic , strong)UIImageView *tipView;

@end

@implementation LGLungFollowTemplateCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self.contentView addSubview:self.titleLabel];
        [self.contentView addSubview:self.lineView];
        [self.contentView addSubview:self.tipView];
        [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.contentView).offset(viewPix(14));
            make.left.equalTo(self.contentView).offset(viewPix(28));
            make.right.equalTo(self.contentView).offset(-viewPix(45));
            make.bottom.equalTo(self.contentView).offset(-viewPix(14));
        }];
        [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.titleLabel);
            make.right.equalTo(self.tipView);
            make.bottom.equalTo(self.contentView);
            make.height.equalTo(@(0.5));
        }];
        [self.tipView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.contentView).offset(-viewPix(28));
            make.centerY.equalTo(self.titleLabel);
            make.width.height.equalTo(@(viewPix(12)));
        }];
    }
    return  self;
}

-(void)setType:(LGPositionType)type{
    [super setType:type];
    self.lineView.hidden = NO;
    if(type == LGPositionTypeTop){
        //第一个
        [self.titleLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.contentView).offset(viewPix(22));
            make.bottom.equalTo(self.contentView).offset(-viewPix(14));
        }];
        
    }else if (type == LGPositionTypeMiddle){
        //中间
        [self.titleLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.contentView).offset(viewPix(14));
            make.bottom.equalTo(self.contentView).offset(-viewPix(14));
        }];
        
    }else if (type == LGPositionTypeBottom){
        //底部
        self.lineView.hidden = YES;
        [self.titleLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.contentView).offset(viewPix(14));
            make.bottom.equalTo(self.contentView).offset(-viewPix(22));
        }];
        
    }else{
        self.lineView.hidden = YES;
        [self.titleLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.contentView).offset(viewPix(22));
            make.bottom.equalTo(self.contentView).offset(-viewPix(22));
        }];
    }
}

-(void)setDataDic:(NSDictionary *)dataDic{
    [super setDataDic:dataDic];
    self.titleLabel.text = LGNSString(dataDic[@"title"]);
}

-(UILabel *)titleLabel{
    if(!_titleLabel){
        _titleLabel = [UILabel lableWithText:@"" colorString:@"#282828" textFont:LGFont(15) textAlignment:NSTextAlignmentLeft lines:0];
    }
    return _titleLabel;
}

-(UIView *)lineView{
    if(!_lineView){
        _lineView = [[UIView alloc]init];
        _lineView.backgroundColor = [UIColor colorWithString:@"#E0E0E0"];
    }
    return _lineView;
}


-(UIImageView *)tipView{
    if(!_tipView){
        _tipView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"grayTip16"]];
    }
    return _tipView;
}
@end
